package cn.diancan.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.Brand;
import cn.diancan.model.Goods;
import cn.diancan.model.types.CommonType;
import cn.diancan.service.BrandService;
import cn.diancan.util.DateUtil;
import cn.diancan.vo.other.Pager;

@Component("brandService")
public class BrandServiceImpl implements BrandService {
    
    @Resource(name="MyBaseDao")
    private MyBaseDao<Brand> brandDao;
    
    @Resource(name="MyBaseDao")
    private MyBaseDao<Goods> goodsDao;
    

    @Override
    public Brand saveBrand(String brandName, String brandLogo)throws OutMsgException,Exception {
	if(null==brandName || "".equals(brandName.trim())){
	    throw new OutMsgException("品牌名不能为空");
	}
	if(null==brandLogo || "".equals(brandLogo.trim())){
	    throw new OutMsgException("品牌名不能为空");
	}
	
	Brand brand = new Brand();
	brand.setBrandName(brandName);
	brand.setBrandLogo(brandLogo);
	brand.setCreateTime(DateUtil.currentDateTime());
	brand.setModifyTime(DateUtil.currentDateTime());
	brand.setIsDelete(CommonType.Delete.NO);
	brandDao.save(brand);
	return brand;
    }
    
    @Override
    public boolean updateBrand(Long id,String brandName,String brandLogo)throws OutMsgException,Exception{
	if(null==brandName || "".equals(brandName.trim())){
	    throw new OutMsgException("品牌名不能为空");
	}
	if(null==brandLogo || "".equals(brandLogo.trim())){
	    throw new OutMsgException("品牌名不能为空");
	}
	Brand brand = findBrandById(id);
	if(null==brand){
	    throw new OutMsgException("该品牌不存在");
	}
	
	brand.setBrandLogo(brandLogo);
	brand.setBrandName(brandName);
	brand.setModifyTime(DateUtil.currentDateTime());
	brandDao.update(brand);
	return true;
    }
    
    
    @Override
    public Brand findBrandById(Long id) throws OutMsgException {
	if(null==id){
	    throw new OutMsgException("参数错误");
	}
	return brandDao.get("FROM Brand WHERE id=? AND isDelete=?", new Object[]{id,CommonType.Delete.NO});
    }
    
    @Override
    public Pager findByPager(Pager pager, String brandName) throws OutMsgException { 
	String hql = "FROM Brand WHERE isDelete=? ";
	List<Object> param =new ArrayList<Object>();
	param.add(CommonType.Delete.NO);
	if(null!= brandName && !"".equals(brandName.trim())){
	    hql+=" AND instr(brandName,?)>0 ";
	    param.add(brandName.trim());
	}
	hql+=" ORDER BY id desc";
	
	List<Brand> brands = brandDao.find(hql, param,pager.getPageNumber(),pager.getPageSize());
	Long count = brandDao.count("SELECT count(id) "+hql, param);
	pager.setList(brands);
	pager.setTotalCount(count.intValue());
	
	return  pager;
    }
    
    
    @Override
    public boolean deleteBrandById(Long id)throws OutMsgException,Exception{
	Brand brand = findBrandById(id);
	if(null==brand){
	    throw new OutMsgException("该品牌不存在");
	}
	//判断产品是否引用
	
	Goods goods =goodsDao.get("FROM Goods WHERE brandId=? AND isDelete=?", new Object[]{id,CommonType.Delete.NO});
	if(null!=goods){
	    throw new OutMsgException("错误操作:该品牌下还有商品");
	}
	
	brand.setIsDelete(CommonType.Delete.YES);
	brandDao.update(brand);
	
	return true;
    }

    @Override
    public List<Brand> findList(Integer pageNo, Integer row) {
	String hql = "FROM Brand WHERE isDelete=? ";
	List<Object> param = new ArrayList<Object>();
	hql+=" ORDER BY id desc";
	param.add(CommonType.Delete.NO);
	List<Brand> list = brandDao.find(hql, param,pageNo, row); 
	return list;
    }
    
    
     
    
    

}
